define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'pici/index' + location.search,
                    add_url: 'pici/add',
                    edit_url: 'pici/edit',
                    del_url: 'pici/del',
                    multi_url: 'pici/multi',
                    import_url: 'pici/import',
                    table: 'pici',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'pici_code', title: __('Pici_code'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'is_fangcuan', title: __('是否是防窜货'), searchList: {"1":__('否'),"2":__('是')}, formatter: Table.api.formatter.normal},
                        {field: 'is_suyuan', title: __('是否是溯源'), searchList: {"1":__('否'),"2":__('是')}, formatter: Table.api.formatter.normal},
                        {field: 'is_activity', title: __('是否是活动码'), searchList: {"1":__('否'),"2":__('是')}, formatter: Table.api.formatter.normal},
                        {field: 'number', title: __('Number')},
                        {field: 'start_number', title: __('Start_number'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'end_number', title: __('End_number'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'state', title: __('State'), searchList: {"0":__('生成中'),"1":__('已完成')}, formatter: Table.api.formatter.normal},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, formatter: Table.api.formatter.status},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        {
                            field: 'operate',
                            width: "150px",
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    title: __('查看防伪码'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    url: 'code/index',
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    }
                                },
                            ],
                            formatter: Table.api.formatter.operate
                        },
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $("input[name='row[is_tao]']").click(function(){
                if($(this).val()==2){
                    $("#tao_number").removeClass("hidden");
                }else{
                    $("#tao_number").addClass("hidden");
                }
            })
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
